<?php
defined('IN_SITE') or exit('Access Denied');

$action = trim($action) ? $action : '';
/*if($action != 'ajaxcheckcode')
{
}*/
$head['keywords'] = $SETTING['keywords'];
$head['description'] = $SETTING['description'];

require_once WWW_ROOT.'/includes/form.class.php';
require_once  'comment.class.php';
$comments = new comment();


 
switch ( $action )
{
	case 'vote':
		$count = $comments->ajaxupdate($field, $id);
		echo ' '.$LANG[$field].'('.$count[$field].')';
	break;
	
	case 'add':
	
		
		
	//禁止游客发表评论
    if(!$_userid) showmessage('请先登陆后在发表',url("?module=member&action=login&forward=".urlencode(URL)));
	
	
        $url = "?module=comment&keyid=$keyid&verify=$verify";
		$content = new_htmlspecialchars($contenttext);
        $content = trim($content);
        if(strlen($content) >= 1000) showmessage('内容太长，最多1000个文字',$url);
		if(empty($content)) showmessage('内容不能为空',$url);
		$keyid = trim($keyid);
		if($comments->add($commentid, $content, $keyid)) showmessage('回复成功',$url);
	break;
	
	case 'comment':
		
		
	//禁止游客发表评论
    if(!$_userid) showmessage('请先登陆后在发表',url("?module=member&action=login&forward=".urlencode(URL)));
	
		$post = $comments->ajaxpost();
		echo $post;
	break;

	case 'addpost':
		
		
	//禁止游客发表评论
    if(!$_userid) showmessage('请先登陆后在发表',url("?module=member&action=login&forward=".urlencode(URL)));
	
		checkcode($checkcodestr, 1, HTTP_REFERER);
		
		$commentinfo['content'] = trim(new_htmlspecialchars($commentinfo['content']));
        //$content = trim($content);
		
        if(strlen($commentinfo['content']) >= 1000)
        {
            showmessage('内容太长，最多1000个文字');
        }
		if(empty($commentinfo['content'])) showmessage('内容不能为空');
        $keyid = trim($keyid);
		
		/*$content, $module, $field, $value , $userid*/
		
		if($comments->addpost($commentinfo))
		//if($comments->addpost($content, $keyid,$userid))
        //showmessage('发表成功', '?module=comment&keyid='.$keyid.'&verify='.$verify);
		//header("location:".url("?module=content&action=view&contentid=$contentid&verify=$verify"));
		showmessage('发表成功', $forward);
	break;
	
    case 'ajaxpost':
		
		
	//禁止游客发表评论
    if(!$_userid) showmessage('请先登陆后在发表',url("?module=member&action=login&forward=".urlencode(URL)));
	
        if(empty($keyid) || !keyid_verify($keyid, $verify)) showmessage('非法操作');
        $content = keyid_get($keyid);
        $title   = $content['title'];
        include template('comment', 'load');
    break;
	
    case 'ajaxcheckcode':
        if($M['enablecheckcode'])
        {
            $code = form::checkcode('checkcode',5);
            echo $code;
        }
        else
        {
            $code = '';
            echo $code;
        }
    break;
	
	// 
	case 'ajax':
		$condition[] = " `module`= '$mod' ";
		$condition[] = " `field` = '$field' ";
		$condition[] = " `value` = '$value' ";
		
		$pagesize = 3;
		$page = isset($page) ? intval($page) : 1;
		$datas = $comments->get_list($condition,$page, $pagesize);
		
		sleep(1.5);
		include template('ajax.html','comment');
		//dump($comments);
	break;
	
	default:
        $keyid = trim($keyid);
        $verify = trim($verify);
        if(empty($keyid) || !keyid_verify($keyid, $verify)) showmessage('非法操作');
		$setting = cache_read('module_comment.php');
		$content = keyid_get($keyid);
		dump($content);
        $url      = $content['url'];
		$head['title'] = $title   = $content['title'];
		$pagesize	= $setting['maxnum'] ? $setting['maxnum'] : 10;
		$page		= isset($page) ? intval($page) : 1;
		$comments = $comments->get_list($keyid,$page, $pagesize);
		dump($comments);
		
		$pages = $comments['pages'];
		
		include template('list.html','comment');//reply
	break;
}

?>